<?php


namespace app\manage\controller;

use app\manage\model\Admin as AdminModel;
use think\Controller;

class Base extends Controller
{
    const SESSION_KEY = 'admin_id';
    protected $admin = null;
    protected $role = null;
    protected $menu = [];

    public function _initialize()
    {
        //先假设存在session
        if (!session(self::SESSION_KEY)) {
            session(null);
            $this->redirect('/manage/login');
        }
        $admin_id = session(self::SESSION_KEY);
        $this->admin = AdminModel::with('role')->where('id', '=', $admin_id)->find();
        $this->role = $this->getRole($admin_id);
        $this->menu = $this->getMenu($admin_id);
    }

    private function getMenu($admin_id)
    {
        return [];
    }

    private function getRole($admin_id)
    {
        return [];
    }

    protected function postData($key = null)
    {
        $key = is_null($key) ? '' : $key . '/a';
        return $this->request->post($key);
    }

    protected function getData($key = null)
    {
        $key = is_null($key) ? '' : $key;
        return $this->request->get($key);
    }

    public function upload_img()
    {
        $file = $this->request->file('img');
        $info = $file->move(ROOT_PATH . 'public/uploads');
        if ($info) {
            $file_name = config('setting.img_prefix') . DS . $info->getSaveName();
            return json($file_name);
        }
    }

    public function uploadFiles()
    {
        $files = $this->request->file('file');
    }
}